﻿Imports System.Security.Cryptography
Imports System.Text
Imports System.IO

Public Class Utility
    Public Shared Sub ShowMessage(ByVal message As String)
        MessageBox.Show(message, My.Resources.UI.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning)
    End Sub
    Private Shared Key() As Byte = {}
    Private Shared IV() As Byte = {38, 55, 206, 48, 28, 64, 20, 16}
    Private Shared stringKey As String = "!5663a#KN"

    Public Shared Function Encrypt(ByVal text As String) As String
        Key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8))
        Dim des As New DESCryptoServiceProvider
        Dim byteArray() As Byte = Encoding.UTF8.GetBytes(text)
        Dim memstream As New MemoryStream
        Dim cryptStream As New CryptoStream(memstream, des.CreateEncryptor(Key, IV), CryptoStreamMode.Write)
        cryptStream.Write(byteArray, 0, byteArray.Length)
        cryptStream.FlushFinalBlock()
        Return Convert.ToBase64String(memstream.ToArray())
    End Function

    Public Shared Function Decrypt(ByVal text As String) As String
        Key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8))
        Dim des As New DESCryptoServiceProvider
        Dim byteArray() As Byte = Convert.FromBase64String(text)
        Dim memstream As New MemoryStream
        Dim cryptStream As New CryptoStream(memstream, des.CreateDecryptor(Key, IV), CryptoStreamMode.Write)
        cryptStream.Write(byteArray, 0, byteArray.Length)
        cryptStream.FlushFinalBlock()
        Return Encoding.UTF8.GetString(memstream.ToArray())
    End Function
End Class
